package org.android;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class GoogleMapView extends MapActivity {

	private double dLatitude;
	private double dLongitude;

	private double aLatitude;
	private double aLongitude;
	
	private List<Overlay> mapOverlays;
	private Drawable departureDrawable;
	private Drawable arrivalDrawable;
	
	private HelloRoadItemizedOverlay departureItemizedOverlay;
	private HelloRoadItemizedOverlay arrivalItemizedOverlay;
	
	private MapView mapView;
	public static final int MAP_ID = Menu.FIRST;
	public static final int SATELLITE_ID = Menu.FIRST + 1;
	public static final int TRAFFIC_ID = Menu.FIRST + 2;
		
    class MapOverlay extends com.google.android.maps.Overlay {
		@Override
		public boolean onTouchEvent(MotionEvent event, MapView mapView) {
			// ---when user lifts his finger---
			if (event.getAction() == 1) {
				GeoPoint p = mapView.getProjection().fromPixels(
						(int) event.getX(), (int) event.getY());

				Geocoder geoCoder = new Geocoder(getBaseContext(), Locale
						.getDefault());
				try {
					List<Address> addresses = geoCoder
							.getFromLocation(p.getLatitudeE6() / 1E6, p
									.getLongitudeE6() / 1E6, 1);

					String add = "";
					if (addresses.size() > 0) {
						for (int i = 0; i < addresses.get(0)
								.getMaxAddressLineIndex(); i++)
							add += addresses.get(0).getAddressLine(i) + "\n";
					}

					Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT)
							.show();
				} catch (IOException e) {
					e.printStackTrace();
				}
				return true;
			} else
				return false;
		}
	}
    
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.googlemap);
		
		Bundle extras = getIntent().getExtras();
		
		if (extras != null) {
			dLatitude = extras.getDouble("dLatitude");
			dLongitude = extras.getDouble("dLongitude");

			aLatitude = extras.getDouble("aLatitude");
			aLongitude = extras.getDouble("aLongitude");
			
			mapView = (MapView) findViewById(R.id.mapview);
			mapView.setBuiltInZoomControls(true);
			mapOverlays = mapView.getOverlays();
			departureDrawable = this.getResources()
					.getDrawable(
					R.drawable.departure);
			arrivalDrawable = this.getResources().getDrawable(
					R.drawable.arrival);
			departureItemizedOverlay = new HelloRoadItemizedOverlay(
					departureDrawable);
			arrivalItemizedOverlay = new HelloRoadItemizedOverlay(
					arrivalDrawable);
			
			GeoPoint dPoint = new GeoPoint((int) (dLatitude * 1E6),
					(int) (dLongitude * 1E6));
			
			GeoPoint aPoint = new GeoPoint((int) (aLatitude * 1E6),
					(int) (aLongitude * 1E6));
		
			Toast
					.makeText(
							getBaseContext(),
							dPoint.getLatitudeE6() / 1E6 + ","
									+ dPoint.getLongitudeE6() / 1E6,
							Toast.LENGTH_SHORT).show();
			Toast
					.makeText(
							getBaseContext(),
							aPoint.getLatitudeE6() / 1E6 + ","
									+ aPoint.getLongitudeE6() / 1E6,
							Toast.LENGTH_SHORT).show();
			
			OverlayItem departureOverlayitem = new OverlayItem(dPoint, "", "");
			OverlayItem arrivalOverlayitem = new OverlayItem(aPoint, "", "");
			departureItemizedOverlay.addOverlay(departureOverlayitem);
			arrivalItemizedOverlay.addOverlay(arrivalOverlayitem);
			mapOverlays.add(departureItemizedOverlay);
			mapOverlays.add(arrivalItemizedOverlay);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, MAP_ID, 0, "Map");
		menu.add(0, SATELLITE_ID, 0, "Satellite");
		menu.add(0, TRAFFIC_ID, 0, "Traffic");
		return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case MAP_ID:
			mapView();
			break;
		case SATELLITE_ID:
			satelliteView();
			break;
		case TRAFFIC_ID:
			trafficView();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void mapView() {
		mapView.setStreetView(true);
		mapView.setSatellite(false);
		mapView.setTraffic(false);
	}
	
	public void satelliteView() {
		mapView.setSatellite(true);
		mapView.setStreetView(false);
		mapView.setTraffic(false);
	}
	
	public void trafficView() {
		mapView.setSatellite(false);
		mapView.setStreetView(false);
		mapView.setTraffic(true);
	}
	
}
